import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 状态
  const currentUser = ref({
    id: null,
    username: '',
    nickname: '',
    email: '',
    phone: '',
    name: '',
    role: '',
    roles: [],
    teacherId: null,
    studentId: null,
    avatar: '',
    organizations: [],
    currentAcademicYear: '',
    currentAcademicYearOnly: '',
    currentYear: null,
    currentSemester: null
  })

  const isLoggedIn = ref(false)

  // 计算属性
  const isTeacher = computed(() => currentUser.value.role === 'teacher')
  const isStudent = computed(() => currentUser.value.role === 'student')
  const isAdmin = computed(() => currentUser.value.role === 'admin')

  // 方法
  const setCurrentUser = (userInfo) => {
    currentUser.value.id = userInfo.id
    currentUser.value.username = userInfo.username || userInfo.name
    currentUser.value.nickname = userInfo.nickname || ''
    currentUser.value.email = userInfo.email || ''
    currentUser.value.phone = userInfo.phone || ''
    currentUser.value.name = userInfo.name || userInfo.nickname
    currentUser.value.role = userInfo.role
    currentUser.value.roles = userInfo.roles || []
    currentUser.value.teacherId = userInfo.teacherId
    currentUser.value.studentId = userInfo.studentId
    currentUser.value.avatar = userInfo.avatar || ''
    currentUser.value.organizations = userInfo.organizations || []
    currentUser.value.currentAcademicYear = userInfo.currentAcademicYear || ''
    currentUser.value.currentAcademicYearOnly = userInfo.currentAcademicYearOnly || ''
    currentUser.value.currentYear = userInfo.currentYear
    currentUser.value.currentSemester = userInfo.currentSemester
    isLoggedIn.value = true
  }

  const clearUser = () => {
    currentUser.value.id = null
    currentUser.value.username = ''
    currentUser.value.nickname = ''
    currentUser.value.email = ''
    currentUser.value.phone = ''
    currentUser.value.name = ''
    currentUser.value.role = ''
    currentUser.value.roles = []
    currentUser.value.teacherId = null
    currentUser.value.studentId = null
    currentUser.value.avatar = ''
    currentUser.value.organizations = []
    currentUser.value.currentAcademicYear = ''
    currentUser.value.currentAcademicYearOnly = ''
    currentUser.value.currentYear = null
    currentUser.value.currentSemester = null
    isLoggedIn.value = false
  }

  const restoreUser = () => {
    const token = localStorage.getItem('token')
    const userInfo = localStorage.getItem('userInfo')
    
    if (token && userInfo) {
      try {
        const parsedUserInfo = JSON.parse(userInfo)
        setCurrentUser(parsedUserInfo)
        return true
      } catch (error) {
        console.error('恢复用户信息失败:', error)
        clearUser()
        return false
      }
    }
    return false
  }

  const saveUser = (userInfo) => {
    setCurrentUser(userInfo)
    localStorage.setItem('userInfo', JSON.stringify({
      id: currentUser.value.id,
      username: currentUser.value.username,
      nickname: currentUser.value.nickname,
      email: currentUser.value.email,
      phone: currentUser.value.phone,
      name: currentUser.value.name,
      role: currentUser.value.role,
      roles: currentUser.value.roles,
      teacherId: currentUser.value.teacherId,
      studentId: currentUser.value.studentId,
      avatar: currentUser.value.avatar,
      organizations: currentUser.value.organizations,
      currentAcademicYear: currentUser.value.currentAcademicYear,
      currentAcademicYearOnly: currentUser.value.currentAcademicYearOnly,
      currentYear: currentUser.value.currentYear,
      currentSemester: currentUser.value.currentSemester
    }))
  }

  const logout = () => {
    clearUser()
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
  }

  // 初始化时尝试恢复用户信息
  restoreUser()

  return {
    // 状态
    currentUser,
    isLoggedIn,
    
    // 计算属性
    isTeacher,
    isStudent,
    isAdmin,
    
    // 方法
    setCurrentUser,
    clearUser,
    restoreUser,
    saveUser,
    logout
  }
}) 